
$(document).ready(init);

function init(){
	
	initData();
	renderPage();
	bindEvent();
	addPrivilegeAttr();
	ComponentUtil.privilegeFilter();
}

function initData(){
	queryMode = _QUERY_MODE_SIMPLE;
}

function bindEvent(){
	$(document).bind(_EVENT_PROMPT_INFO,ComponentUtil.promptMsg);
	$(document).bind(_EVENT_PROMPT_WARNING,ComponentUtil.promptMsg);
	$(document).bind(_EVENT_PROMPT_ERROR,ComponentUtil.promptMsg);
	
	$('#showAdvancedQueryFormLink').click(ComponentUtil.showAdvancedQueryForm);
	$('#hideAdvancedQueryFormLink').click(ComponentUtil.hideAdvancedQueryForm);
	
	$('#simpleQueryBt').click(sendQueryResult);
	$('#advanceQueryBt').click(sendQueryResult);
	
}

function renderPage(){
	
	var addBt = {text:'新增管理员',iconCls:'icon-add',handler:popupAddWindow,id:'addAdminInfoBt'};
	var removeBt = {text:'删除管理员',iconCls:'icon-cancel',handler:confirmRemove,id:'removeAdminInfoBt' };
	var blockBt = {text:'封锁管理员',iconCls:'icon-no',handler:blockAdmins };
	var activateBt = {text:'激活管理员',iconCls:'icon-ok',handler:activateAdmins };
	var resetPasswordBt = {text:'重置密码',iconCls:'icon-reload',handler:resetPassword };
	
	$("#adminTable").datagrid({
		striped: true,
		fitColumns:true,
		pagination:true,
		idField:'id',
		frozenColumns:[[
	        {field:'ck',checkbox:true}
	    ]],
	    sortName:'id',
    	sortOrder:'desc',
		url:webappName+'/admin/adminInfoManager/listAdmin',
		toolbar:[addBt,'-',removeBt,'-',blockBt,'-',activateBt,'-',resetPasswordBt,'-'],
		loadMsg:'加载数据中,请稍等...',
		rowStyler:rowRender,
		onDblClickRow:popupModifyWindow
	});
	ComponentUtil.localizePagination($('#adminTable').datagrid('getPager'));
	
	$("#roleTable").datagrid({
		striped: true,
		fitColumns:true,
		pagination:false,
		idField:'code',
		frozenColumns:[[
	        {field:'ck',checkbox:true}
	    ]],
	    sortName:'id',
    	sortOrder:'desc',
		url:webappName+'/admin/roleManager/listRole',
		loadMsg:'加载数据中,请稍等...'
	});
	
	$("#adminDetailDiv").dialog({
		width:550,
		height:380,
		resizable:false,
		closed:true
	});
	$.messager.defaults = {ok:'确认',cancel:'取消'};
	
}


function addPrivilegeAttr(){
	ComponentUtil.addPrivilegeAttr($('#addAdminInfoBt'),'PRI_ADD_ADMININFO');
	ComponentUtil.addPrivilegeAttr($('#removeAdminInfoBt'),'PRI_REMOVE_ADMININFO');
}



function rowRender(rowIndex,rowData){
	if (rowData.status == 9){
		return 'background-color:#ffdddd';
	}
}

function statusRender(value,rowData,rowIndex){
	if (value == 0){
		return '有效';
	}else if(value == 9){
		return '无效';
	}else{
		return '未知('+value+')';
	}
}

function popupAddWindow(){
	var addButton = {
		text:'新增管理员',
		iconCls:'icon-add',
		handler:addAdminInfo
	};
	
	var cancelButton = {
		text:'取消',
		handler:closeDetailWindow
	};
	
	
	$("#adminDetailDiv").dialog({
		closed:false,
		modal:true,
		buttons:[addButton,cancelButton]
	});
	$('#password').css('visibility','visible');
	$('#passwordLb').css('visibility','visible');
	$('#loginName').attr('disabled',false);
	
	$('#loginName').val('');
	$('#password').val('');
	$('#roleTable').datagrid('clearSelections');
	$('#roleTable').datagrid('reload');
}

function addAdminInfo(){
	if (!$('#adminForm').form('validate')) return;
	
	var roles = '';
	var rows = $('#roleTable').datagrid('getSelections');
	for(var i=0;i<rows.length;i++){
		roles += rows[i].code+',';
	}
	if (roles.length>0){
		roles = roles.substring(0,roles.length-1);
	}
	var loginName= $.trim($('#loginName').val());
	var password= $.trim($('#password').val());
	
	var adminInfo={loginName:loginName,roles:roles,password:password};
	$.post(webappName+"/admin/adminInfoManager/addAdmin",$.param(adminInfo),onAddAdminComplete,"json");
	closeDetailWindow();
}

function onAddAdminComplete(data){
	$(document).trigger(_EVENT_PROMPT_INFO,"操作成功");
	$("#adminTable").datagrid('reload');
}

function popupModifyWindow(rowIndex, rowData){
	
	var saveButton = {
		text:'保存信息',
		iconCls:'icon-save',
		handler:function(){commitAdminUpdate(rowData.id);}
	};
	
	var cancelButton = {
		text:'取消',
		handler:closeDetailWindow
	};
	
	$("#adminDetailDiv").dialog({
		closed:false,
		modal:true,
		buttons:[saveButton,cancelButton]
	});
	
	$('#roleTable').datagrid('clearSelections');
	$('#roleTable').datagrid('reload');
	$('#password').css('visibility','hidden');
	$('#passwordLb').css('visibility','hidden');
	$('#loginName').attr('disabled',true);
	
	$('#loginName').val(rowData.loginName);
	
	var roles = rowData.roles.split(",");
	for(var i=0;i<roles.length;i++){
		$("#roleTable").datagrid('selectRecord',roles[i]);
	}
	
}

function commitAdminUpdate(adminId){
	var rolesSelected = $('#roleTable').datagrid('getSelections');
	var roles='';
	for(var i=0;i<rolesSelected.length;i++){
		roles += rolesSelected[i].code+',';
	}
	if (roles.length>0){
		roles = roles.substring(0,roles.length-1);
	}
	
	var adminUpdate = {id:adminId,roles:roles};
	$.post(webappName+"/admin/adminInfoManager/updateAdmin",$.param(adminUpdate),onUpdateAdminComplete,"json");
}

function onUpdateAdminComplete(responseVO){
	var result = responseVO.executeResult;
	if (result){
		$(document).trigger(_EVENT_PROMPT_INFO,"更新成功");
	}else{
		$(document).trigger(_EVENT_PROMPT_ERROR,ComponentUtil.getErrorDesc(responseVO.errCode));
	}
	$("#adminTable").datagrid('reload');
	closeDetailWindow();
}

function closeDetailWindow(){
	$("#adminDetailDiv").dialog('close');
}

function confirmRemove(){
	var rows = $('#adminTable').datagrid('getSelections');
	if (rows.length==0){
		$(document).trigger(_EVENT_PROMPT_INFO,'请选择需要删除的项目');
		return;
	}
	$.messager.confirm('确认','确认要删除吗?',removeAdmins);
}

function removeAdmins(isConfirm){
	if (!isConfirm) return;
	
	var rows = $('#adminTable').datagrid('getSelections');
	
	var admin;
	var adminList='';
	for(var i=0;i<rows.length;i++){
		admin = rows[i];
		adminList += admin.id + ',';
	}
	adminList = adminList.substring(0,adminList.length-1);
	var requestPara = 'adminList='+adminList;
	
	$.post(webappName+"/admin/adminInfoManager/removeAdmins",requestPara,onRemoveAdminComplete,"json");
	
	
}
function blockAdmins(){
	var rows = $('#adminTable').datagrid('getSelections');
	if (rows.length==0){
		$(document).trigger(_EVENT_PROMPT_INFO,'请选择需要封锁的项目');
		return;
	}
	
	var admin;
	var adminList='';
	for(var i=0;i<rows.length;i++){
		admin = rows[i];
		adminList += admin.id + ',';
	}
	adminList = adminList.substring(0,adminList.length-1);
	var requestPara = 'adminList='+adminList;
	
	$.post(webappName+"/admin/adminInfoManager/blockAdmins",requestPara,onBlockAdminComplete,"json");
	
}

function activateAdmins(){
	var rows = $('#adminTable').datagrid('getSelections');
	if (rows.length==0){
		$(document).trigger(_EVENT_PROMPT_INFO,'请选择需要激活的项目');
		return;
	}
	
	var admin;
	var adminList='';
	for(var i=0;i<rows.length;i++){
		admin = rows[i];
		adminList += admin.id + ',';
	}
	adminList = adminList.substring(0,adminList.length-1);
	var requestPara = 'adminList='+adminList;
	
	$.post(webappName+"/admin/adminInfoManager/activateAdmins",requestPara,onActivateAdminComplete,"json");
	
}
function resetPassword(){
	var rows = $('#adminTable').datagrid('getSelections');
	if (rows.length==0){
		$(document).trigger(_EVENT_PROMPT_INFO,'请选择需要重置密码的项目');
		return;
	}
	
	var admin;
	var adminList='';
	for(var i=0;i<rows.length;i++){
		admin = rows[i];
		adminList += admin.id + ',';
	}
	adminList = adminList.substring(0,adminList.length-1);
	var requestPara = 'adminList='+adminList;
	
	$.post(webappName+"/admin/adminInfoManager/resetPassword",requestPara,onResetPasswordComplete,"json");
	
	
}

function onRemoveAdminComplete(responseVO){
	
	$("#adminTable").datagrid('reload');
	
	var result = responseVO.executeResult;
	if (result){
		$(document).trigger(_EVENT_PROMPT_INFO,"操作成功");
	}else{
		$(document).trigger(_EVENT_PROMPT_ERROR,ComponentUtil.getErrorDesc(data.errCode));
	}
	
}

function onBlockAdminComplete(responseVO){
	$("#adminTable").datagrid('reload');
	
	var result = responseVO.executeResult;
	if (result){
		$(document).trigger(_EVENT_PROMPT_INFO,"操作成功");
	}else{
		$(document).trigger(_EVENT_PROMPT_ERROR,ComponentUtil.getErrorDesc(data.errCode));
	}
}

function onActivateAdminComplete(responseVO){
	$("#adminTable").datagrid('reload');
	
	var result = responseVO.executeResult;
	if (result){
		$(document).trigger(_EVENT_PROMPT_INFO,"操作成功");
	}else{
		$(document).trigger(_EVENT_PROMPT_ERROR,ComponentUtil.getErrorDesc(data.errCode));
	}
}

function onResetPasswordComplete(responseVO){
	$("#adminTable").datagrid('reload');
	var defaultPwd = responseVO.objects.defaultPassword;
	var result = responseVO.executeResult;
	if (result){
		$(document).trigger(_EVENT_PROMPT_WARNING,"操作成功,初始密码("+defaultPwd+")");
	}else{
		$(document).trigger(_EVENT_PROMPT_ERROR,ComponentUtil.getErrorDesc(data.errCode));
	}
}

function sendQueryResult(){
	var queryParams = $('#adminTable').datagrid('options').queryParams;
	
	queryParams.queryMode = queryMode;
	queryParams.simpleQueryString = $.trim($('#simpleQueryConditionTxt').val());
	queryParams.loginName = $.trim($('#loginNameTxt').val());
	queryParams.roles = $.trim($('#roleTxt').val());
	
	$("#adminTable").datagrid('load');  
	
}
